
#Set up

#s1
rm(list=ls())

setwd ("C:/Users/cecidy/Google Drive/ZH_analysis/")

source("ZHfunctions.R")
options(scipen=9999)

ZeroHunger <- read.csv("Data/ZHdf/ZeroHungerDF_2020.csv",header=TRUE, sep=",")

#s2
################################################################################################################

#start1_

#BF and Infant mortality 00 to 10 robustness sample

#######################################################################################################################

#Make a nonBF ZHvariable!

summary(ZeroHunger$BFPerCapTotal00to10for2000Analysis1000)
summary(ZeroHunger$ZHPerCapTotal00to10for2000Analysis1000)

ZeroHunger$ZHPerCapTotal00to10for2000Analysis1000NOBF <- with(ZeroHunger,ZHPerCapTotal00to10for2000Analysis1000-BFPerCapTotal00to10for2000Analysis1000)

#######################################################################################################################

#1. Prepare each dataframe

#Get Rural municipalities, dont include those very large municipalities
Poverty04 <- subset(ZeroHunger, PopDensity20002000Analysis<=150 & SizeKm22000Analysis<=10000)

#Get list of vars
load(file = "Data/ForSamples/MPI00list.rda")
load(file = "Data/ForSamples/Basic00.rda")

RuralPoverty04 <- subset(Poverty04, select=c("BFPerCapTotal00to10for2000Analysis1000","ZHPerCapTotal00to10for2000Analysis1000NOBF",Basic00,MPI00List,
                                             "CensusInfMort2000for2000Analysis100","CensusInfMor2010for2000Analysis100"))
names(RuralPoverty04)

RuralPoverty04 <- na.omit(RuralPoverty04)

#Then take away the ones affected by rural credit
load(file = "Data/ForSamples/EcludeForRCProb00.rda")

RuralPoverty04 <- subset(RuralPoverty04,(!(IBGECode7digit %in% EcludeForRCProb00)))

#Just copy to get the "quality" dataset
RuralPoverty04quality <- RuralPoverty04

#############################################################################################

#Make logged variables

#First make MPI with the correct 0s
test <- NrOfZeroALL(RuralPoverty04quality)#This gives me all the vars with 0s
MPIadd <- test[[1]]
Otheradd <- test[[2]]

#For the MPI because Ive not previously renamed when adding I just override the vars with 0 with the minimum/2
WithAdded <- lapply(MPIadd,function(y) AddConstant(RuralPoverty04quality,y,addName=""))
WithAdded <- data.frame(do.call("cbind",WithAdded))

RuralPoverty04quality[,MPIadd] <- WithAdded[,MPIadd]#just override

#Then I need to make the MPI - here I need to use the 00 function
RuralPoverty04quality <- MakeMPI00(RuralPoverty04quality)

#######################################################################################################################

#Then for the others I cbind them on, and because Ive not included any of previous noCero there no duplicates
RuralPoverty04quality <- cbind(RuralPoverty04quality,lapply(Otheradd,function(y) AddConstant(RuralPoverty04quality,y,addName="nocero")))#

###########################################################################################################################

#And finally I need to log variables - get a list of all names

OtheraddCero <- paste(Otheradd,"nocero",sep="")#here which are names no cero

#All these need log
cols.log <- c("BFPerCapTotal00to10for2000Analysis1000","ZHPerCapTotal00to10for2000Analysis1000NOBF","MPI2000for2000Analysis","MPI2010for2000Analysis",
              "GDPPerCapPublicrealN2000for2000Analysis1000",OtheraddCero,"RemoteMinPopFifty2000Analysis",
              "CensusInfMor2010for2000Analysis100","CensusInfMort2000for2000Analysis100",
              "PopDensity20002000Analysis","SizeKm22000Analysis","MeanSlopefor2000analysis","MeanElevationfor2000analysis")
addlog <- function(x) log10(x)
Log10variables <- data.frame(sapply(RuralPoverty04quality[cols.log],addlog))
colnames(Log10variables) <- paste(colnames(Log10variables), "log10",sep="")

#Get all onto ZH
RuralPoverty04quality <- cbind(RuralPoverty04quality,Log10variables)
head(RuralPoverty04quality)

###########################################################################################################################

#Take away the redundandt
Log10variables <- NULL
Poverty04 <- NULL
WithAdded <- NULL
ZeroHunger <- NULL

#############################################################################################

#I make it on log here but that I might have to go back and change after checking for log or not!
#For BF I LOG first (then if I want to go back and do normal to check later ok BUT I already did that for poverty00-10 and it was the same!)
RuralPoverty04quality<- getDataFrame(RuralPoverty04quality,"contr.Sum",
                                     contrastVar1="stateName",contrastVar2="Biomefor2000Analysis",setRefToMostCommonLevel,
                                     BaseVar="BFPerCapTotal00to10for2000Analysis1000log10",
                                     MainName="BFPerCapTotal00to10for2000Analysis1000log10scaledMain")

#fin1_
###############################################################################################################

#start2_

#Prepare vectors of variables for regressions

###############################################################################################################

Depvar <- "CensusInfMor2010for2000Analysis100"
DepVarLog <- "CensusInfMor2010for2000Analysis100"

IndepVarLin <- "BFPerCapTotal00to10for2000Analysis1000log10scaledMain"#already scaled so dont need to do it again

baselineDepVar <- "scale(CensusInfMort2000for2000Analysis100)"

stateName <- "stateName"

intLin <- c("BFPerCapTotal00to10for2000Analysis1000log10scaledMain","stateName")
intLin <- paste(intLin,collapse="*")

xvars <- c("scale(MPI2000for2000Analysis)",
           "scale(GDPPerCapPublicrealN2000for2000Analysis1000log10)",
           "scale(TotalKcal2000percapperdaynocerolog10)",
           "scale(TotalHectareCrops2000for2000Analysisnocerolog10)","scale(TotalHectarePasture2006for2000Analysisnocerolog10)",
           "scale(TotalHectareFarmsLess502006for2000Analysisnocerolog10)","scale(RemoteMinPopFifty2000Analysislog10)",
           "scale(ChangeCummulativeSPEI98and00to08and10for2000Analysis)",
           "scale(TotRCpcNOPRONAF00to10for2000Analysis1000nocerolog10)",
           "scale(MeanElevationfor2000analysislog10)","scale(MeanSlopefor2000analysislog10)",
           "scale(PopDensity20002000Analysislog10)","scale(SizeKm22000Analysislog10)","scale(ZHPerCapTotal00to10for2000Analysis1000NOBFlog10)",
           "Biomefor2000Analysis")

#For CBPS models
cbpsDepVar <- "BFPerCapTotal00to10for2000Analysis1000log10"
cbpsbaselineDepVar <- "CensusInfMort2000for2000Analysis100"

cbpsVars <- c("MPI2000for2000Analysis",
              "GDPPerCapPublicrealN2000for2000Analysis1000log10","TotalKcal2000percapperdaynocerolog10",
              "TotalHectareCrops2000for2000Analysisnocerolog10","TotalHectarePasture2006for2000Analysisnocerolog10",
              "TotalHectareFarmsLess502006for2000Analysisnocerolog10","RemoteMinPopFifty2000Analysislog10",
              "ChangeCummulativeSPEI98and00to08and10for2000Analysis",
              "TotRCpcNOPRONAF00to10for2000Analysis1000nocerolog10",
              "MeanElevationfor2000analysislog10","MeanSlopefor2000analysislog10",
              "PopDensity20002000Analysislog10","SizeKm22000Analysislog10","ZHPerCapTotal00to10for2000Analysis1000NOBFlog10",
              "stateName","Biomefor2000Analysis")

#########################################################################################################################


#fin2_

#temp1
#temp2

################################################################################################################################

